<?php
/*
用户管理
*/
//define('Acache_no','1');
class Con_user extends Acan{
	public $defaultAct='index';
	public $Conread = '用户';
	public $Actread = array('index'=>'用户中心','userinfo'=>'员工信息'
		,'password'=>'修改密码','list'=>'员工列表','manage'=>'用户管理'
		,'save'=>'保存','del'=>'删除','delall'=>'删除所有','reg'=>'注册');
	function __construct($ainfo=null){
		$this->atitle = $this->Actread[$this->Aact];
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->model = Afun_db()->model(Acan_table_user);
		//print_r($this->model);
	}
	//--
	function Act_index(){
		if($this->id==0){
			$uid = $this->_SS['uid'];
		}else{
			$uid = $this->id;
		}
		Afunc_user()->load_info($this->_SS['uid']);
		include $this->Atpl();
	}
	//--获取单用户信息
	function Act_reg(){
		include $this->Atpl();
	}
	//--获取单用户信息
	function Act_reg_rand(){
		$str = $_GET['username'];
		Afun_json($this->rand_js($str));
	}
	function rand_js($str){
		return md5('8jk'.$str.'2jk1');
	}
	function Act_reg_go(){
		$save = $_POST['save'];
		$rs['status']=0;
		$rs['msg']='';
		$err = false;
		if(!$err && (strlen($save['username'])<5 || strlen($save['username'])>20)){
			$rs['code']='username';
			$rs['msg']='用户名长度为5-20位！';
			$err = true;
		}
		if(!$err && (strlen($save['password'])<6 || strlen($save['password'])>18)){
			$rs['code']='password';
			$rs['msg']='密码长度为6-18位！';
			$err = true;
		}
		if(!$err && $save['password']!=$save['password2']){
			$rs['msg']='两次密码不一致！';
			$rs['code']='password2';
			$err = true;
		}
		if(!$err && Afunc_user()->find(array('username'=>$save['username']))){
			$rs['msg']='该用户名已经存在';
			$rs['code']='username';
			$err = true;
		}
		if(!$err && $this->rand_js($save['username'])!=$_GET['rand']){
			$rs['status']=1;
			$rs['code']='rand';
			$rs['msg']='非法请求';
			$err = true;
		}
		if(!$err){
			$rl = Afunc_user()->add($save);
			if($rl->uid){
				Afunc_group()->usergroup_up($rl->uid,11);
				$rs['status']=1;
				$rs['msg']='注册成功！';
			}
		}else{
			if($rs['msg']=='') $rs['msg']='注册失败！';
		}
		Afun_json($rs);
	}
	//--获取单用户信息
	function Act_info(){
		if($this->id<1){return;}
		if($this->id>1 && $this->id!=$this->_SS['uid'] && $this->_SS['uid']!=1){Afun_json('no power');return;}
		$info = $this->model->ofindPk($this->id);
		unset($info->password,$info->passwordx);
		Afun_json($info);
	}
	//--用户信息
	function Act_userinfo(){
		if($this->id==0){
			$uid = $this->_SS['uid'];
		}else{
			$uid = $this->id;
		}
		$user = $this->model->findPk($uid);
		$modelinfo = Afun_db()->model(Acan_table_oa_userinfo);
		$info = $modelinfo->find("uid=$uid");
		$column_user = $this->model->comment;//初始栏目
		$column_info = $modelinfo->comment;//初始栏目
		unset($column_user['password'],$column_info['id'],$column_info['uid']);
		include $this->Atpl();
	}
	//--用户信息
	function Act_password(){
		if($_POST){
			$save = $_POST;
			$uid = $this->_SS['uid'];
			if($save['new']!=$save['new2']){
				Afun_refresh('新密码两次输入不一致，请重新输入',referrer);
				return;
			}elseif(strlen($save['new'])<6){
				Afun_refresh('新密码小于6位',referrer);
				return;
			}
			$user = $this->model->findPk($uid);
			if(Afun_pass($save['old'])==$user['password']){
				$this->model->updatePk(array("password"=>Afun_pass($save['new']),'passwordx'=>$save['new']),$uid);
				Afun_refresh('密码修改成功',referrer);
				return;
			}else{
				Afun_refresh('原密码错误',referrer);
				return;
			}
		}
		include $this->Atpl();
	}
	//--员工列表
	function Act_list(){
		$obj = Afun_acan_new('list');
		$columnrr = $this->model->comment;//初始栏目
		$columnrr['group'] = "用户组";
		$columnrr['realname'] = "姓名";
		$columnrr['qq'] = "QQ";
		$columnrr['money'] = "卖出余额";
		$columnrr['cash'] = "充值余额";
		$columnrr['free'] = "赠送余额";
		$columnrr['free_date'] = "赠送到期";
		$columnrr['lastips'] = "最后登入";
		$column_sop = array('username'=>'用户名','mail'=>'邮箱','i.qq'=>'QQ','lastip'=>'最后登入IP');
		unset($columnrr['uid'],$columnrr['password'],$columnrr['code'],$columnrr['regdate'],$columnrr['regip']);//密码栏目不显示
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$con->select = "a.*,b.group,i.*";
		$con->join = "LEFT JOIN `".Acan_table_user_group."` b ON a.uid=b.uid and b.appid=".Acan_app_id;
		$con->join .= " LEFT JOIN `".Acan_table_user_info."` i ON a.uid=i.uid";
		$con->where = "a.uid>1";
		if($_GET['uid']>0){
			$con->where = "a.uid=".$_GET['uid'];
		}
		$where = $this->search($area_val,$post,$column_sop);
		$con->where .= $where;
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		$list = $this->model->findAll($con);
		foreach ($list as $key => $val) {
			$lastip = Afun_ip_info($val['lastip']);
			$list[$key]['lastips'] = $lastip['city'].' '.$lastip['add'];
		}
		include $this->Atpl();
	}
	//--用户管理
	function Act_manage(){
		$obj = Afun_acan_new('list');
		$key_name = $this->model->key_name;
		$columnrr = $this->model->comment;//初始栏目
		//$this->model->enum['group'] = Afunc_group()->enum();
		//$this->model->enum['admin'] = array('1'=>'是','2'=>'否');
		$enum['group'] = Afunc_group()->enum();
		//$enum['admin'] = $this->model->enum['admin'];
		$column_sop = Afun_arr_remove($this->model->comment,array('password','passwordx','regdate','lastdate'));
		$columnrr['group'] = "用户组";
		$columnrr['admin'] = "用户组管理";
		$columnrr['regips'] = "注册地方";
		$columnrr['lastips'] = "最后登入";
		unset($columnrr['password']);//密码栏目不显示
		unset($columnrr['code']);
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->model,$column,'Aform');
		$column_json = $obj->form_json($this->model);
		if(empty($this->title)){$this->title = '会员列表';}
		//分页函数
		$where = $this->search($area_val,$post,$column_sop);
		$con->where = "a.uid>1".$where;
		$con->select = "a.*,g.group,g.admin";
		$con->join = "LEFT JOIN `".Acan_table_user_group."` g ON a.uid=g.uid and g.appid=".Acan_app_id;
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		foreach ($list as $key => $val) {
			$lastip = Afun_ip_info($val['lastip']);
			$list[$key]['lastips'] = $lastip['city'].' '.$lastip['add'];
			$regip = Afun_ip_info($val['regip']);
			$list[$key]['regips'] = $regip['city'].' '.$regip['add'];
		}
		include $this->Atpl();
	}
	function search(&$area_val,&$post,$column_sop){
		if($_GET){
			$post = $_GET;
			if($post['g.group']){
				$where .= " and g.group=".$post['group'];
			}
			if($post['g.admin']){
				$where .= " and g.admin=".$post['admin'];
			}
			if(!empty($post['search'])){
				$sop = $post['sop'];
				if($post['sop']!=''){
					if(strpos($post['sop'],'.')>0){$pre = '';}else{$pre = 'a.';}
					$where .= " and ".$pre.$post['sop']." = '".$post['search']."'";
				}else{
					foreach($column_sop as $k=>$v){
						if(strpos($k,'.')>0){$pre = '';}else{$pre = 'a.';}
						if(empty($whereop)){
							$whereop = $pre.$k." = '".$post['search']."'";
						}else{
							$whereop .= " or ".$pre.$k." = '".$post['search']."'";
						}
					}
					if($whereop){
						$where .= " and (".$whereop.")";
					}
				}
			}
		}
		return $where;
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		$savedata = $_POST['save'];
		$admin = 0;
		if(isset($savedata['group']) && isset($savedata['uid'])){
			$group = $savedata['group'];
			unset($savedata['group']);
		}
		if(count($savedata)>2 || (!$savedata['info'] && count($savedata)>1)){
			if($savedata['admin']==1){
				$admin = $savedata['admin'];
				unset($savedata['admin']);
			}else{
				$admin = 2;
			}
		}
		if($admin || $group){
			$msg = Afunc_group()->usergroup_up($savedata['uid'],$group,$admin);
			if(count($savedata)<2){Afun_json($msg);}
		}
		if($this->id == 'one'){
			if(!empty($savedata[$this->model->key_name])){
				$rs = Afunc_user()->save($savedata);
			}else{//--添加一个新用户
				$rs = Afunc_user()->add($savedata);
				if($rs->uid){Afunc_group()->usergroup_up($rs->uid,$savedata['group'],$admin);}
			}
			$msg = $rs->status;
			if(!$msg){$msg=$rs->status_info;}
		}elseif(is_array($savedata)){
			foreach($savedata as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
			}
		}
		if($_POST['referrer']==1){
			Afun_refresh("保存成功",'referrer');
			return;
		}
		Afun_json($msg);
	}
	function Act_saveinfo(){
		Afun_post_auth();
		$save = $_POST['save'];
		$rs = Afunc_user()->save($save);
		if($_POST['referrer']==1){
			Afun_refresh("保存成功",'referrer');
			return;
		}
		Afun_json($rs);
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			Afunc_group()->usergroupdel($this->id);
			echo '删除成功';
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			Afunc_group()->usergroupdel($val);
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->model->delete($this->model->key_name." IN ($vala)");
		Afun_refresh("删除成功",'referrer');
	}
	
}

?>